package org.caenv.webapp.components.beans;

import java.util.List;

import org.richfaces.model.TreeNode;

public class TreeMenuBeanFactory {

	private TreeNode menuNodes = new TreeMenuBean();

	private String[] projects = { 
			"projeto-teste-1",
			"projeto-teste-2",
			"projeto-teste-4",
			
		};

	public TreeMenuBeanFactory () {
		
		TreeNode projectsTreeNode = new TreeMenuBean("Projects", null, false);
		TreeNode robotsTreeNode = new TreeMenuBean("Robots", null, false);
		TreeNode repositoriesTreeNode = new TreeMenuBean("Repositories", null, false);
		
		
		addChilds(projectsTreeNode, this.projects);
		addChilds(robotsTreeNode, this.projects);
		addChilds(repositoriesTreeNode, this.projects);
	
		// Build the 
		int idx = 0;
		
		getMenuNodes().addChild(idx++, projectsTreeNode);
		getMenuNodes().addChild(idx++, robotsTreeNode);
		getMenuNodes().addChild(idx++, repositoriesTreeNode);

	}

	private void addChilds(TreeNode projectsTreeNode, String[] childs) {
		for (int i = 0; i < childs.length; i++){
		    TreeNode child = new TreeMenuBean(projects[i], "/pages/state/newState.xhtml", true);
		    projectsTreeNode.addChild(i, child);
		}
	}
	
	private void addChilds(TreeNode projectsTreeNode, List childs) {
		for (int i = 0; i < childs.size(); i++){
			if(Math.random() < 0.5){
				TreeNode child = new TreeMenuBean(childs.get(i).toString(), "/pages/teste.xhtml", true);
				projectsTreeNode.addChild(i, child);
			}else{
				TreeNode child = new TreeMenuBean(childs.get(i).toString(), "/pages/teste2.xhtml", true);
				projectsTreeNode.addChild(i, child);
			}
		}
	}

	public void setMenuNodes(TreeNode stationNodes) {
		this.menuNodes = stationNodes;
	}

	public TreeNode getMenuNodes() {
		return menuNodes;
	}
	
}
